# Maintainer: Christian Rebischke <chris.rebischke@archlinux.org>
# Contributor: ml <>
pkgname=cmctl
pkgver=1.10.1
pkgrel=1
pkgdesc='Automatically provision and manage TLS certificates in Kubernetes'
arch=('loong64' 'x86_64' 'aarch64')
url='https://github.com/jetstack/cert-manager'
license=('Apache')
depends=('kubectl')
makedepends=('git' 'go')
groups=('kubectl-plugins')
source=("$url/archive/v$pkgver/$pkgname-$pkgver.tar.gz")
sha512sums=('2e255fd7becbde2e46243d224b0d6c1ad09777ac8e84bac1834aed669331ea4ac74d2aa22f26b915e9661160c50c80dd924da70255bfa8a98a9e7a636a4813fc')
b2sums=('1386b625a349413059833e986d74d82bf9b99946561e7bee995b6b4f0869a13563b5d88fc81f84630b6dab024e350582d7fb776133965762a19cbc48a62c874c')

build() {
  local _x _commit
  _commit=$(bsdcat "$pkgname-$pkgver.tar.gz" | git get-tar-commit-id)
  _x=(
    AppVersion="v$pkgver"
    AppGitCommit="${_commit:?}"
    AppGitState="clean"
  )

  cd cert-manager-"$pkgver"
  export CGO_ENABLED=1
  export CGO_LDFLAGS="$LDFLAGS"
  export CGO_CFLAGS="$CFLAGS"
  export CGO_CPPFLAGS="$CPPFLAGS"
  export CGO_CXXFLAGS="$CXXFLAGS"
  export GOFLAGS='-buildmode=pie -trimpath -modcacherw -mod=readonly'
  go build -ldflags="-linkmode=external ${_x[*]/#/-X=github.com/jetstack/cert-manager/pkg/util.} 
  -X github.com/jetstack/cert-manager/cmd/ctl/pkg/build.name=cmctl 
  -X github.com/jetstack/cert-manager/cmd/ctl/pkg/build/commands.registerCompletion=true" ./cmd/ctl
}

check() {
  cd cert-manager-"$pkgver"
  # we don't want e2e and other tests that explicitly want bazel
  go test -short ./cmd/ctl/...
}

package() {
  cd cert-manager-"$pkgver"
  install -Dm755 ctl "$pkgdir"/usr/bin/cmctl

  # build bash completions
  #mkdir -p "${pkgdir}/usr/share/bash-completion/completions"
  #"$pkgdir"/usr/bin/cmctl completion bash > "${pkgdir}/usr/share/bash-completion/completions/cmctl"

  ## build zsh completions
  #mkdir -p "${pkgdir}/usr/share/zsh/site-functions"
  #"$pkgdir"/usr/bin/cmctl completion zsh > "${pkgdir}/usr/share/zsh/site-functions/_cmctl"

  ## build fish completions
  #mkdir -p "${pkgdir}/usr/share/fish/vendor_completions.d/"
  #"$pkgdir"/usr/bin/cmctl completion fish > "${pkgdir}/usr/share/fish/vendor_completions.d/cmctl.fish"
}
